/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ar.com.neolokura.travian.helper.browser.admin;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.WebClient;
import com.zenkey.net.prowser.Prowser;
import com.zenkey.net.prowser.Tab;
import java.net.URISyntaxException;
import org.apache.log4j.Logger;

/**
 *
 * @author gustavo
 */
public class BrowserAdmin {

    private static Logger logger = Logger.getLogger(BrowserAdmin.class.getSimpleName());
    private static BrowserAdmin INSTANCE = null;
    private static WebClient webClientInstance = null;

    // Private constructor suppresses 
    private BrowserAdmin() {
        logger.info(">> BrowserAdmin started...");
    }

    // creador sincronizado para protegerse de posibles problemas  multi-hilo
    // otra prueba para evitar instanciación múltiple 
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new BrowserAdmin();
        }
    }

    public static BrowserAdmin getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    // Creates WebClient Singleton
    public WebClient getWebClientInstance() {
        if (webClientInstance == null) {
            createWebClientInstance();
        }
        webClientInstance.setThrowExceptionOnScriptError(false);
        webClientInstance.setCssEnabled(false);
        webClientInstance.setPrintContentOnFailingStatusCode(false);
        webClientInstance.setThrowExceptionOnFailingStatusCode(false);
        return webClientInstance;
    }

    private synchronized static void createWebClientInstance() {
        if (webClientInstance == null) {
            webClientInstance = new WebClient(BrowserVersion.FIREFOX_3_6);
            logger.info(">> WebClient instantiated...");
        }
    }

    public Tab getProwserLogin(String userName, String password, Tab tab) throws URISyntaxException{
        
        ProwserHandler prowserHandler = new ProwserHandler();
        tab = prowserHandler.loginProwser(userName, password, tab);
        return tab;
    }
    
    public Prowser getProwserInstance() {
        return new Prowser();
    }

    public Tab getTabInstance() {
        return getProwserInstance().createTab();

    }
}
